/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.GroupRequest;

import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Contains utility methods to create various types of GEF Requests.
 * 
 * @author frankd@ifi.uio.no
 */
public class RequestUtils {

    private RequestUtils() {
        // private to disallow instantiation
    }

    /**
     * Creates a request to move the given editpart, which must have a model of type
     * GraphicalElement.
     */
    @SuppressWarnings("unchecked")
    public static Request createMoveRequest(final EditPart ep, final Rectangle to) {
        GraphNode node = (GraphNode) ep.getModel();
        Rectangle from = new Rectangle(node.getPosition(), node.getSize());

        Point moveDelta = new Point(to.x - from.x, to.y - from.y);
        Dimension sizeDelta = to.getSize().getDifference(from.getSize());

        ChangeBoundsRequest moveRequest = new ChangeBoundsRequest();
        moveRequest.setType(RequestConstants.REQ_MOVE);
        moveRequest.setEditParts(ep);
        moveRequest.setLocation(to.getLocation());
        moveRequest.setMoveDelta(moveDelta);
        moveRequest.setSizeDelta(sizeDelta);
        return moveRequest;
    }

    /**
     * Creates a request to delete the given editpart.
     */
    public static Request createDeleteRequest(final EditPart editPart) {
        GroupRequest delRequest = new GroupRequest();
        delRequest.setEditParts(editPart);
        delRequest.setType(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST);
        return delRequest;

    }
}
